var fs = require('fs-promise');
var path = require('path');

var basePath = path.resolve(__dirname, '..') + '/';
var argv = Array.prototype.concat.apply([], process.argv);

var releaseType = argv[2] || 'dev';

resPath = basePath;
if (releaseType !== 'dev') {
	resPath += 'output/';
}
console.log('releaseType', releaseType);
var modulePath = resPath + 'node_modules/';
var transList = [modulePath + 'antd-mobile'];

fs.readdir(modulePath).then(function(list) {
	list.forEach(function(name) {
		if (name.indexOf('rmc-') === 0) {
			transList.push(modulePath + name);
		}
	});
	return Promise.all(transList.map(function(walkPath) {
		return fs.walk(walkPath + '/lib').then(function(list) {
			var allfiles = {};
			var webfiles = list.map(function(item) {
				allfiles[item.path] = item;
				return item.path;
			}).filter(function(path) {
				return path.substr(-7) === '.web.js';
			});

			var plist = webfiles.map(function(path) {
				var indexPath = path.slice(0, -7) + path.slice(-3);
				var nativePath = path.slice(0, -7) + '.native' + path.slice(-3)
				if (indexPath in allfiles && !(nativePath in allfiles)) {
					console.log(path, indexPath, nativePath)
					return fs.move(indexPath, nativePath).then(function() {
						return fs.copy(path, indexPath);
					});
				}
			});

			return Promise.all(plist)
		});
	}))
}).then(function() {
	console.log('trans ' + transList + ' done');
});
